package org.catspaw.cherubim.security.ss3;

import java.util.Collection;
import java.util.HashSet;
import java.util.Set;

import org.catspaw.cherubim.security.Subject;
import org.catspaw.cherubim.security.rbac.User;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.GrantedAuthorityImpl;
import org.springframework.security.core.userdetails.UserDetails;

public class SpringSecuritySubject implements Subject, UserDetails {

	private User							user;
	private Collection<GrantedAuthority>	authorities;
	private Set<String>						authorityCodes;

	public SpringSecuritySubject(User user, Set<String> authorityCodes) {
		if (user == null) {
			throw new IllegalArgumentException("user must not be null");
		}
		this.user = user;
		this.authorityCodes = authorityCodes;
		authorities = new HashSet<GrantedAuthority>();
		for (String code : authorityCodes) {
			authorities.add(new GrantedAuthorityImpl(code));
		}
	}

	public String getUsername() {
		return user.getUsername();
	}

	public String getPassword() {
		return user.getPassword();
	}

	public Collection<GrantedAuthority> getAuthorities() {
		return authorities;
	}

	public boolean isAccountNonExpired() {
		return true;
	}

	public boolean isAccountNonLocked() {
		return true;
	}

	public boolean isCredentialsNonExpired() {
		return true;
	}

	public boolean isEnabled() {
		return true;
	}

	public User getUser() {
		return user;
	}

	public Set<String> getAuthorityCodes() {
		return authorityCodes;
	}
}
